package com.jpa.service.impl;

import com.jpa.entity.sign.NmBaseProjectDbinfo;
import com.jpa.entity.sign.User;
import com.jpa.repository.sign.NmBaseProjectDbinfoRepository;
import com.jpa.repository.sign.UserRepository;
import com.jpa.service.SignUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class SignUserServiceImpl implements SignUserService {

    @Autowired
    UserRepository userRepository;

    @Autowired
    NmBaseProjectDbinfoRepository nmBaseProjectDbinfoRepository;

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public List<User> users(List<Integer> ids) {
        return userRepository.users(ids);
    }

    @Transactional(value = "signPlatformTransactionManager")
    @Override
    public int insertUser() {
        int i = 0;
        List<User> all = userRepository.findAll();
        Optional<NmBaseProjectDbinfo> byId = nmBaseProjectDbinfoRepository.findById(1);
        NmBaseProjectDbinfo dd = byId.get();
        for (User u : all) {
            User uu = new User();
            uu.setUserName("手动" + u.getUserName());
            uu.setAge(u.getAge());
            userRepository.save(uu);

            NmBaseProjectDbinfo ss = new NmBaseProjectDbinfo();
            ss.setDbAddress(dd.getDbAddress());
            ss.setDbUser(dd.getDbUser());
            ss.setDbName(dd.getDbName());
            ss.setDbPassword(dd.getDbPassword());
            nmBaseProjectDbinfoRepository.save(ss);

            if (i == 1) {
                i = 1/0;
            }
            i++;
        }
        return 0;
    }


    @Transactional(value = "signPlatformTransactionManager")
    @Override
    public int insertUsers() {
        List<User> all = userRepository.findAll();
        Optional<NmBaseProjectDbinfo> byId = nmBaseProjectDbinfoRepository.findById(1);
        NmBaseProjectDbinfo dd = byId.get();
        NmBaseProjectDbinfo ss = new NmBaseProjectDbinfo();
        ss.setDbAddress(dd.getDbAddress());
        ss.setDbUser(dd.getDbUser());
        ss.setDbName(dd.getDbName());
        ss.setDbPassword(dd.getDbPassword());
        nmBaseProjectDbinfoRepository.save(ss);

        int i = 6/0;

        for (User u : all) {
            User uu = new User();
            uu.setUserName("手动" + u.getUserName());
            uu.setAge(u.getAge());
            userRepository.save(uu);
        }
        return 0;
    }
}
